package com.example.demo1.repository;

import com.example.demo1.pojo.user;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface userRepository extends JpaRepository<user, Integer> {
    Optional<user> findByUsername(String username);

    Optional<user> findByEmail(String email);

    Optional<user> findByPhone(String phone);

    boolean existsByUsername(String username);

    boolean existsByEmail(String email);

    boolean existsByPhone(String phone);

    @Query("select count(u) > 0 from user u where u.username = :username and u.id <> :id")
    boolean existsByUsernameAndIdNot(@Param("username") String username, @Param("id") Integer id);

    @Query("select count(u) > 0 from user u where u.email = :email and u.id <> :id")
    boolean existsByEmailAndIdNot(@Param("email") String email, @Param("id") Integer id);

    @Query("select count(u) > 0 from user u where u.phone = :phone and u.id <> :id")
    boolean existsByPhoneAndIdNot(@Param("phone") String phone, @Param("id") Integer id);
}
